#Author:Math2Gold ( http://groups.google.com/group/math2gold ) 
#Last update:2009 Sep 11

import datetime
import xml.dom.minidom
from xml.dom.minidom import getDOMImplementation
from google.appengine.ext import db
from google.appengine.api import users
import urllib
import urllib2
import urlparse
import default_m2g_conf
import m2g_misc
import time
import logging
import cgi, cgitb
import logging

form = cgi.FieldStorage()

url=default_m2g_conf.atom

print 'Content-Type:text/plain'
print ''

try:
    if urllib.unquote(form.getvalue('hub.topic')) == url and form.getvalue('hub.mode')=='subscribe' :
        cgi.sys.stdout.write(form.getvalue('hub.challenge'))
        logging.info(form.getvalue('hub.challenge'))
except:
    pass

def getText(nodelist):
    rc = ""
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc = rc + node.data
    return rc

class RSSDataMark(db.Model):
  desc = db.StringProperty(required=True)

user_agent = 'Math2Gold Messager'
headers = { 'User-Agent' : user_agent }
req = urllib2.Request(default_m2g_conf.rss,None,headers)
op = urllib2.build_opener()
response = urllib2.urlopen(req)
document = response.read()
dom = xml.dom.minidom.parseString(document)

fftime=time.time();

m=dom.getElementsByTagName("rss")[0].getElementsByTagName("channel")[0].getElementsByTagName("item");

ncount=0;
mresult=[];

import hashlib

for x in m:
    mid=getText(x.getElementsByTagName("guid")[0].childNodes)
    d=hashlib.sha1(mid);
    keyn=d.hexdigest()
    exq =RSSDataMark.get_by_key_name(keyn)
    if    not ( exq == None) :
        continue    
    rtitle=getText(x.getElementsByTagName("title")[0].childNodes)
    rdesc=getText(x.getElementsByTagName("description")[0].childNodes)
    e=RSSDataMark(desc=rtitle,key_name=keyn)
    e.put()
    ncount=ncount+1
    mresult=[rtitle]+mresult;

for x in mresult:
    logging.info("Send:"+x)
    m2g_misc.sendMBlog(x.encode("UTF-8"),
                               default_m2g_conf.twitter_username,
                               default_m2g_conf.twitter_password,
                      uri=default_m2g_conf.twitter_senduri,
                      prefix='status=')    
